/*
 * DG (DG Gaming Engine)  - Server
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.tictactoe.server;

import com.dg.gaming.api.common.*;
import com.dg.gaming.server.*;
import com.dg.gaming.server.controller.ServerTableController;
import com.dg.gaming.tictactoe.common.*;
import nanoxml.*;

/**
 * This is the server table parser for a game of checkers.
 *
 * @author Bob Marks
 * @version Alpha 0.2.3
 */
public class TictactoeServerController extends ServerTableController {

    /**
     * Constructor for a game of tictactoe.
     *
     * @param gameKey
     */
    public TictactoeServerController(ServerTableHandler tblController) {
        super(tblController);
    }

    /**
     * Create a new model when the game starts.
     *
     * @see ocom.dg.gaming.server.ServerController#startGame(int)
     */
    public void startGame(String tableNum) {
        GameModel model = getTable(tableNum).getModel();
        if (model == null) {
            model=new TicTacToeModel();
            getTable(tableNum).setModel(model);
        }
        setModel(tableNum, model);
    }

    /**
     * Parse a checkers message.
     *
     * @see ocom.dg.gaming.server.ITableParser#parseTableMessage(ocom.dg.gaming.server.ServerConnectionThread, nanoxml.XMLElement)
     */
    public void parseTableMessage(ServerTableHandler conn,
                                  XMLElement message, String tableNum) {
        // Retrieve the message type
        String messageType = message.getName();

        // Parse the message
        if (messageType.equals(MsgTicTacToeMove.XML_NAME)) {
            // unpack message
            MsgTicTacToeMove move = new MsgTicTacToeMove(message);

            movePiece(conn, tableNum, move);
        }
    }

    /**
     * Move the tic-tac-toe piece.
     *
     * @param conn      Connection to server.
     * @param tableNum  Table number.
     * @param move      TicTacToe move.
     */
    private void movePiece(ServerTableHandler conn, String tableNum,
                           MsgTicTacToeMove move) {
        // retrieve table num and username
        long username = move.getUserid();

        // Update server model
        TicTacToeModel model = (TicTacToeModel) getModel(tableNum);
        model.setData(move.getX(), move.getY(), move.getValue());

        // Trasmit to other players
        conn.transmitToTablePlayers(username, tableNum, move);

        // Check to see if the game is over
        checkGameOver(conn, tableNum);
    }

    /**
     * Check to see if the game is over or not.
     *
     * @param conn       Connection to the server.
     * @param model      TicTacToe model.
     * @param tableNum   Table number.
     */
    private void checkGameOver(ServerTableHandler conn, String tableNum) {
        // Status is either -1, DRAW or WIN
        int resultType = -1;
        int curSeatNum = getSeatNum(conn.getUserId(), tableNum);
        TicTacToeModel model = (TicTacToeModel) getModel(tableNum);
        boolean isGameOver = model.isGameWon(curSeatNum);
        if (isGameOver) {
            resultType = IGameOver.WIN;
        } else if (model.isNoCellsLeft()) {
            resultType = IGameOver.DRAW;
        }

        // Create game over object if a win or draw
        if (resultType != -1 && conn != null) {
            // Update the server data
            gameOver(conn, tableNum, conn.getUserId(), resultType);
        }
    }

    public void gameOver(ServerTableHandler conn, String tableNum,
                         int resultType) {
        // TODO Auto-generated method stub

    }
}
